const { CONFIG } = require("./config");
const { sendMsg, getWeather, getWords } = require("./api");
const { togetherDays, getBirthday, getColor } = require("./util/index");

/**
 * 云函数入口
 * @param {*} event
 * @param {*} context
 */
exports.main_handler = async (event, context) => {
  const weatherData = (await getWeather()).data.now;
  // 城市
  const city = CONFIG.CITY;
  // 天气状况
  const weather = weatherData.text;
  // 实时温度
  const nowTemp = weatherData.temp + "℃";
  // 恋爱天数
  let loveDays = await togetherDays(CONFIG.START_DATE);
  if (loveDays % 365 === 0 || loveDays % 366 === 0) {
    loveDays = "恋爱纪念日快乐";
  }
  loveDays = loveDays + "天";
  // 生日天数
  let birthday = await getBirthday(CONFIG.BIRTHDAY);
  if (birthday === 0) {
    birthday = "生日快乐";
  }
  birthday = birthday + "天";

  // 彩虹屁
  const word = (await getWords()).data.data.text;
  // 随机颜色
  const color = await getColor();

  // 组装数据
  let data = {
    city: {
      value: city,
    },
    weather: {
      value: weather,
    },
    nowTemp: {
      value: nowTemp,
    },
    loveDays: {
      value: loveDays,
    },
    birthday: {
      value: birthday,
    },
    word: {
      value: word,
      color: color,
    },
  };
  await sendMsg(data);
  return "成功！！！";
};
